Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    unit: {
      type: Number,
      value: 1000 // 每次兑换所需星票数（如100或1000）
    },
    starCount: {
      type: Number,
      value: 0 // 用户当前的星星票数量
    }
  },
  data: {
    count: 0 // 当前兑换次数
  },
  methods: {
    increase() {
      const { count } = this.data;
      const { unit, starCount } = this.properties;
      const totalCost = (count + 1) * unit;
      if (totalCost > starCount) {
        wx.showToast({ title: '星票不足', icon: 'none' });
        return;
      }
      this.setData({ count: count + 1 });
    },
    decrease() {
      const { count } = this.data;
      if (count > 0) {
        this.setData({ count: count - 1 });
      }
    },
    cancel() {
      this.setData({ count: 0 }); // 重置
      this.triggerEvent('cancel');
    },
    confirm() {
      const { count } = this.data;
      const { unit, starCount } = this.properties;
      const cost = count * unit;
      if (cost > starCount || count === 0) {
        wx.showToast({ title: '兑换次数不足或为0', icon: 'none' });
        return;
      }
			this.setData({ count: 0 }); // 成功后重置
			wx.showToast({
				title: '兑换成功!',
			})
      this.triggerEvent('confirm', {
        count,
        cost
      });
    }
  }
});
